# add valuations to the world
from rai.logger.logger import *
    
def addEntityToWorld(entity, world):
    """ Add a entity to the valuation in the world
        only if it does not already exist
        e.g. 'john => b1'
    """
    if entity in world.getVal():
        debug("warning: '"+ entity +"' exists")
        # how is 'subject' defined in the domain?
        var = world.getVal()[entity]
        rhv = var
    else:
        debug("warning: '"+ entity + "' does not exist")
        # convert the subject to a variable (e.g. john => b1)
        var = world.getDomainWrapper().getNewVar()
        # add it to the valuation
        world.getVal()[entity] = var
        rhv = var
    return rhv

def addPropertyWithVarEntities(property, entity_list, world):
    """
        add a propery with a variable number of entities
    """
    if property in world.getVal():
        debug("warning: '"+property+"' exists")
        tuple_p = tuple(entity_list)
        world.getVal()[property].add(tuple_p)
    else:
        debug("warning: '"+property+"' does not exist")
        # add property to valuation
        tuple_p = tuple(entity_list)
        world.getVal()[property] = set()
        world.getVal()[property].add(tuple_p)          

def addPropertyWith2Entities(property, first_entity, second_entity, world):
    """ add a property with two entities:
        in => {(b1, n)}
    """
    if property in world.getVal():
        debug("warning: '"+property+"' exists")
        #print val4[property]
        # convert to tuple
        tuple_p = (first_entity, second_entity)
        world.getVal()[property].add(tuple_p)
        #print val4[np_property]
    else:
        debug("warning: '"+property+"' does not exist")
        # add property to valuation
        tuple_p = (first_entity, second_entity)
        world.getVal()[property] = set()
        world.getVal()[property].add(tuple_p)    
    
def addEntityPropertyToWorld(entity, property, world):
    """ add a property to the world
        e.g. singer => {b1}
    """
    if property in world.getVal():
        debug("warning: '"+property+"' exists")
        #print val4[property]
        # convert to tuple
        tuple_p = (entity,)
        world.getVal()[property].add(tuple_p)
        #print val4[np_property]
    else:
        debug("warning: '"+property+"' does not exist")
        # add property to valuation
        tuple_p = (entity,)
        world.getVal()[property] = set()
        world.getVal()[property].add(tuple_p)
